package com.sx.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.model.Msg;
import com.dfec.utils.Consists;
import com.dfec.utils.PropertiesUtil;
import com.dfec.utils.ResponseUtil;
import com.sx.service.ServeProductService;

/**
 * Servlet implementation class UserDataServlet
 */
@WebServlet(name = "sps", description = "服务产品", urlPatterns = { "/sps" })
public class ServeProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private List<Object> list=null;
    Msg msg=null;
    ServeProductService s=new ServeProductService();
	
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		msg=new Msg();
		list=new ArrayList<>();
		
		String type = request.getParameter("type");
		try {
			if (type.equals("list")) {
				String position=request.getParameter("position");
				String first_class=request.getParameter("first_class");
				String second_class=request.getParameter("second_class");
				int page=Integer.parseInt(request.getParameter("page"));
				int size=Integer.parseInt(request.getParameter("size"));
				String keyWord =request.getParameter("keyWord");
				
				List<Object> getdata=s.getProduct(position, first_class, second_class, page, size,keyWord);
				if(!getdata.get(0).toString().equals("[]")){
					msg.setCode(Consists.DATA_Code_OK);
					msg.setMsg(Consists.DATA_Msg_OK);
				}else {
					msg.setCode(Consists.DATA_Code_OK__NODATA);
					msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
				}
				list.add(msg);
				list.add(getdata.get(0));
				
				 ResponseUtil.sendScript(response, JSON.toJSONString(list));
			}else if(type.equals("diteals")) {
					String id=request.getParameter("id");
					String product_type=request.getParameter("product_type");
					String file_path=request.getParameter("file_path");
					String title=request.getParameter("title");
					if(product_type.equals("html")) {
						List<Object> getdata=s.getProductDiteals(id);
						if(!getdata.get(0).toString().equals("[]")){
							msg.setCode(Consists.DATA_Code_OK);
							msg.setMsg(Consists.DATA_Msg_OK);
						}else {
							msg.setCode(Consists.DATA_Code_OK__NODATA);
							msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
						}
						list.add(msg);
						list.add(getdata.get(0));
						
						 ResponseUtil.sendScript(response, JSON.toJSONString(list));
					}else if(product_type.equals("pdf")) {
						File file = new File(PropertiesUtil.JDBC_PDF+file_path+"/"+title+".pdf");
						
						File outFile=null;
						FileInputStream inputStream=null;
						if (!file.exists()) {
							String filePath = ServeProductServlet.class.getClassLoader().getResource("/error.pdf").getPath();
							outFile=new File(filePath);
						}else {
							outFile=file;
						}
						inputStream=new FileInputStream(outFile);
						int i=inputStream.available();
//						System.out.println(file.getAbsolutePath()+"i="+i);
						byte data[]=new byte[i];
						int responsedata = inputStream.read(data);
						inputStream.close();
						
						response.setContentType("application/pdf");
						response.getOutputStream().write(data);;
					}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
			list.add(msg);
			list.add(new ArrayList<>());
			ResponseUtil.sendScript(response, JSON.toJSONString(list));
		}
		
		
	}

}
